package com.excel.service.excel;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ExcelImportProcessorFactory implements InitializingBean, ApplicationContextAware {
    private Map<String, ExcelImportProcessor> map = new HashMap<>();
    private ApplicationContext context;

    @Override
    public void afterPropertiesSet() {
        String[] beanNames = context.getBeanNamesForType(ExcelImportProcessor.class);
        for (String beanName : beanNames) {
            ExcelImportProcessor bean = context.getBean(beanName, ExcelImportProcessor.class);
            if(ObjectUtil.isNull(bean.getTag())){
                continue;
            }
            map.put(bean.getTag(), bean);
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context=applicationContext;
    }


    public ExcelImportProcessor getMessageHandler(String tag) {
        return map.get(tag);
    }
}
